/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.live.client;

import com.medusa.gruul.live.constant.CommonConstant;
import com.netflix.ribbon.proxy.annotation.Http;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

@Slf4j
public class HttpClient {

    public static String doGet(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;

        try {
            // 创建远程url连接对象
            URL url = new URL(httpurl);

            // 通过远程url连接对象打开一个连接，强转成httpURLConnection类
            connection = (HttpURLConnection) url.openConnection();

            //connection.setRequestProperty("X-Token",ReqContext.getToken());
            // 设置连接方式：get
            connection.setRequestMethod(CommonConstant.GET);

            // 设置了解主机服务器的超时时间：15000毫秒
            connection.setConnectTimeout(CommonConstant.FIFTEENSECONDS);

            // 设置了远程返回的数据时间6000毫秒
            connection.setReadTimeout(CommonConstant.SIXSECONDS);

            // 发送请求
            connection.connect();

            // 通过connection连接，获取输入流
            if (connection.getResponseCode() == CommonConstant.TWENTY) {

                // 封装输入流is，并制定字符集
                is = connection.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                // 存放数据
                StringBuffer sbf = new StringBuffer();
                String temp = null;

                // 将接受到数据存入StringBuffer
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
//                    sbf.append("\r\n");
                }

                result = sbf.toString();
            }
        } catch (Exception e) {
            log.error("[post ] error", e);
        } finally {

            //关闭BufferReader
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //关闭InputStream
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            connection.disconnect();
        }

        return result;
    }

    public static String doGetNoTokenHead(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;

        try {
            // 创建远程url连接对象
            URL url = new URL(httpurl);

            // 通过远程url连接对象打开一个连接，强转成httpURLConnection类
            connection = (HttpURLConnection) url.openConnection();

            // 设置连接方式：get
            connection.setRequestMethod(CommonConstant.GET);

            // 设置了解主机服务器的超时时间：15000毫秒
            connection.setConnectTimeout(CommonConstant.FIFTEENSECONDS);

            // 设置了远程返回的数据时间6000毫秒
            connection.setReadTimeout(CommonConstant.SIXSECONDS);

            // 发送请求
            connection.connect();

            // 通过connection连接，获取输入流
            if (connection.getResponseCode() == CommonConstant.TWENTY) {

                // 封装输入流is，并制定字符集
                is = connection.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                // 存放数据
                StringBuffer sbf = new StringBuffer();
                String temp = null;

                // 将接受到数据存入StringBuffer
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
//                    sbf.append("\r\n");
                }

                result = sbf.toString();
            }
        } catch (Exception e) {
            log.error("[post ] error", e);
        } finally {

            //关闭BufferReader
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //关闭InputStream
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            connection.disconnect();
        }

        return result;
    }

    public static String doPost(String httpUrl, String param) {

        HttpURLConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            // 设置连接方式：get
            connection.setRequestMethod(CommonConstant.POST);

            // 设置了解主机服务器的超时时间：15000毫秒
            connection.setConnectTimeout(CommonConstant.FIFTEENSECONDS);

            // 设置了远程返回的数据时间6000毫秒
            connection.setReadTimeout(CommonConstant.SIXSECONDS);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            connection.setRequestProperty("Accept", "application/json;charset=UTF-8");
            // 测试用报文头
            //connection.setRequestProperty("X-Token", ReqContext.getToken());

            // 通过连接对象获得一个输出流
            os = connection.getOutputStream();
            // 通过输出流对象将数据写出去/传输出去，通过字节数组写出
            os.write(param.getBytes("UTF-8"));
            // 通过连接对象获取一个输入流，向远程读取
            if (connection.getResponseCode() == CommonConstant.TWENTY) {

                is = connection.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                StringBuffer sbf = new StringBuffer();
                String temp = null;

                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                }

                result = sbf.toString();
            }
        } catch (Exception e) {
            log.error("[post ] error", e);
        }  finally {
            // 关闭BufferReader
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            // 关闭OutputStream
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            // 关闭InputStream
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            connection.disconnect();
        }

        return result;
    }
}
